package com.example.test.filetr;

import com.alibaba.fastjson.JSON;
import com.example.test.common.P;
import com.example.test.common.SaveContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebFilter(filterName = "loginFilter",urlPatterns = "/*")//过滤未登录就进入内部的请求
public class LoginFilter implements Filter {
    //路径匹配器，支持通配符
    public static final AntPathMatcher PATH_MATCHER=new AntPathMatcher();
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        response.setContentType("text/html;charset=UTF-8");

//        filterChain.doFilter(request,response);

        //获取请求url
        String requestURI = request.getRequestURI();

        String[] urls=new String[]{//不需要处理的url
                "/demo/ok",
                "/empoyee/login",
                "/buck/page/login.html",
                "/index.html",
                "/user/login",
                "/front/page/login.html",
                "/front/page/register.html",
                "/user/register",
        };
        //判断是否要处理
        boolean check = check(urls, requestURI);
        //不需要就放行
        if(check){
            log.info("不处理"+requestURI);
            filterChain.doFilter(request,response);
            return;
        }
        //判断是否员工登录
        if(request.getSession().getAttribute("empoyee")!=null){
//            log.info("用户已登录");
            //用下面这行代码代替原来的代码(Long)userPo.get("id"),直接转换为Long类型
            Long eid= ((Integer) request.getSession().getAttribute("empoyee")).longValue();

            SaveContext.setCurrentId(eid);
            filterChain.doFilter(request,response);
            return;
        }
        //判断是否用户登录
        if(request.getSession().getAttribute("user")!=null){
            Long uid=((Integer) request.getSession().getAttribute("user")).longValue();
            SaveContext.setCurrentId(uid);//保存uid
            filterChain.doFilter(request,response);
            return;
        }

//        //未登录返回未登录,想页面响应数据

        ServletOutputStream outputStream = response.getOutputStream();
//        System.out.println(JSON.toJSONString(P.error("未登录")));
        outputStream.write(JSON.toJSONString(P.error("未登录")).getBytes("UTF-8"));


//        response.getWriter().write(JSON.toJSONString(P.error("未登录")));//不能用了
        log.info("用户未登录");
        response.sendRedirect("/index.html");
        outputStream.close();


    }
    public boolean check(String[] urls,String requestURI){
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
            if(match){
                return true;
            }
        }
        return false;
    }
}
